package com.psycho.sys.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.psycho.sys.api.common.Result;
import com.psycho.sys.api.entity.Announcement;
import com.psycho.sys.api.service.AnnouncementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公告管理控制器
 */
@Tag(name = "公告管理接口")
@RestController
@RequestMapping("/api/announcements")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    @Operation(summary = "分页查询公告列表")
    @GetMapping("/page")
    public Result<Page<Announcement>> page(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "公告标题") @RequestParam(required = false) String title,
            @Parameter(description = "公告类型") @RequestParam(required = false) Integer type,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        return Result.success(announcementService.pageAnnouncements(current, size, title, type, status));
    }

    @Operation(summary = "获取公告详情")
    @GetMapping("/{id}")
    public Result<Announcement> getById(@PathVariable Long id) {
        return Result.success(announcementService.getById(id));
    }

    @Operation(summary = "创建公告")
    @PostMapping
    public Result<Boolean> create(@RequestBody Announcement announcement) {
        return Result.success(announcementService.createAnnouncement(announcement));
    }

    @Operation(summary = "更新公告")
    @PutMapping("/{id}")
    public Result<Boolean> update(
            @PathVariable Long id,
            @RequestBody Announcement announcement) {
        announcement.setId(id);
        return Result.success(announcementService.updateAnnouncement(announcement));
    }

    @Operation(summary = "删除公告")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.success(announcementService.deleteAnnouncement(id));
    }

    @Operation(summary = "批量删除公告")
    @DeleteMapping("/batch")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids) {
        return Result.success(announcementService.batchDeleteAnnouncements(ids));
    }

    @Operation(summary = "更新公告状态")
    @PutMapping("/{id}/status")
    public Result<Boolean> updateStatus(
            @PathVariable Long id,
            @Parameter(description = "状态（0-禁用，1-启用）") @RequestParam Integer status) {
        return Result.success(announcementService.updateAnnouncementStatus(id, status));
    }

    @Operation(summary = "获取最新公告")
    @GetMapping("/latest")
    public Result<List<Announcement>> getLatest(
            @Parameter(description = "获取数量") @RequestParam(defaultValue = "5") Integer limit) {
        return Result.success(announcementService.getLatestAnnouncements(limit));
    }

    @Operation(summary = "获取有效公告")
    @GetMapping("/active")
    public Result<List<Announcement>> getActive(
            @Parameter(description = "公告类型") @RequestParam(required = false) Integer type) {
        return Result.success(announcementService.getActiveAnnouncements(type));
    }
}